package com.bgds.sys.common.utils;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author lj
 * 日期工具类
 */
public class DateUtils {

    /**
     * 获取固定间隔时刻集合
     *
     * @param startDate 开始时间
     * @param endDate   结束时间
     * @param interval  时间间隔(单位：分钟)
     * @return
     */
    public static List<Long> getIntervalTimeList(Date startDate, Date endDate, int interval) {
        List<Long> list = new ArrayList<>();
        while (startDate.getTime() <= endDate.getTime()) {
            list.add(startDate.getTime());
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(startDate);
            calendar.add(Calendar.MILLISECOND, interval);
            if (calendar.getTime().getTime() > endDate.getTime()) {
                if (!startDate.equals(endDate)) {
                    list.add(endDate.getTime());
                }
                startDate = calendar.getTime();
            } else {
                startDate = calendar.getTime();
            }

        }
        return list;

    }

    /**
     * 时间转字符串
     *
     * @param format
     * @param date
     * @return
     */
    public static String convertDate2String(String format, Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(date);
    }

    /**
     * 得到当前时间的前N小时
     *  
     *
     * @return
     */
    public static String getBeforeByHourTime(int hour) {

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - hour);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df.format(calendar.getTime());

    }

    /**
     * 当前时间多少小时以前的时间戳
     *
     * @param hour
     * @return
     */
    public static long getBeforeTimeInMillis(int hour) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - hour);
        return calendar.getTimeInMillis();
    }

    /**
     * 某个时间多少小时以前的时间戳
     *
     * @param hour
     * @return
     */
    public static long getBeforeTimeInMillis(Date date, int hour) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - hour);
        return calendar.getTimeInMillis();
    }

    /**
     * 字符串转时间
     *
     * @param source
     * @return
     */
    public static Date stringConvertDate(String source) {
        //1.定义我们要转成的日期类型（格式是：yyyy-MM-dd HH:mm:ss）,
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            //2.进行转化，转化成功直接返回
            return simpleDateFormat.parse(source);
        } catch (ParseException e) {

            e.printStackTrace();
        }
        //3.如果转化失败返回null
        return null;
    }

    /**
     * 字符串转时间
     *
     * @param source
     * @return
     */
    public static Date stringConvertDate(String format, String source) {
        //1.定义我们要转成的日期类型（格式是：yyyy-MM-dd HH:mm:ss）,
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        try {
            //2.进行转化，转化成功直接返回
            return simpleDateFormat.parse(source);
        } catch (ParseException e) {

            e.printStackTrace();
        }
        //3.如果转化失败返回null
        return null;
    }

    /**
     * 获取当前时间字符串
     *
     * @return
     */
    public static String getCurrentDate(String format) {
        //1.定义我们要转成的日期类型（格式是：yyyy-MM-dd HH:mm:ss）,
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);

        return simpleDateFormat.format(new Date());
    }

    /**
     * 获取固定间隔时刻集合
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @param interval  时间间隔(单位：分钟)
     * @return
     */
    public static List<Object> getIntervalTimeList(String startTime, String endTime, int interval) {
        Date startDate = stringConvertDate(startTime);
        Date endDate = stringConvertDate(endTime);

        List<Object> list = new ArrayList<>();
        while (startDate.getTime() <= endDate.getTime()) {
            list.add(convertDate2String("yyyy-MM-dd HH:mm:ss", startDate));
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(startDate);
            calendar.add(Calendar.MINUTE, interval);
            if (calendar.getTime().getTime() > endDate.getTime()) {
                if (!startDate.equals(endDate)) {
                    list.add(convertDate2String("yyyy-MM-dd HH:mm:ss", endDate));
                }
                startDate = calendar.getTime();
            } else {
                startDate = calendar.getTime();
            }

        }
        return list;


    }

    /**
     * 获取固定间隔时刻集合
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @param interval  时间间隔(单位：毫秒)
     * @return
     */
    public static List<String> getIntervalTimeList1(String startTime, String endTime, int interval) {
        Date startDate = stringConvertDate(startTime);
        Date endDate = stringConvertDate(endTime);
        List<String> list = new ArrayList<>();
        while (startDate.getTime() <= endDate.getTime()) {
            list.add(convertDate2String("yyyy-MM-dd HH:mm:ss:SSS", startDate));
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(startDate);
            calendar.add(Calendar.MILLISECOND, interval);
            if (calendar.getTime().getTime() > endDate.getTime()) {
                if (!startDate.equals(endDate)) {
                    list.add(convertDate2String("yyyy-MM-dd HH:mm:ss:SSS", endDate));
                }
                startDate = calendar.getTime();
            } else {
                startDate = calendar.getTime();
            }

        }
        return list;


    }

    /**
     * 时间戳转字符串
     *
     * @param dateFormat
     * @param millSec
     * @return
     */
    public static String transferLongToDate(String dateFormat, Long millSec) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        Date date = new Date(millSec);
        return sdf.format(date);
    }

    /**
     * UTC日期格式转时间戳
     *
     * @param utcTime
     * @return
     */
    public static long utcToTimeMillis(String utcTime) {
//        String utc = utcTime.replace("Z", " UTC");
//        SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
//        Date d = null;
//        try {
//            d = format.parse(utcTime);
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
//        return d.getTime();
//        String STANDARD_DATE_FORMAT_UTC = "yyyy-MM-dd'T'HH:mm:ss'Z'";
//        SimpleDateFormat sdf = new SimpleDateFormat(STANDARD_DATE_FORMAT_UTC);
//        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
//        try {
//            return   sdf.parse(utcTime).getTime();
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
//        return 0;
        StringBuilder sb = new StringBuilder(utcTime);
        if (!utcTime.contains(".")) {
            sb.insert(utcTime.indexOf("Z"), ".000");
        }
        SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        df2.setTimeZone(TimeZone.getTimeZone("UTC"));
        Date date = null;
        try {
            date = df2.parse(sb.toString());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date.getTime();

    }


    public static String utcTime(long time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        Date date = new Date(time);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - 8);
        return format.format(calendar.getTime());
    }
}
